package com.campusbackend.controller;

import com.campusbackend.entity.Result;
import com.campusbackend.service.FileUploadService;
import com.campusbackend.utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 文件上传控制器
 * 
 * @author yezi
 * @since 2025-07-27
 */
@Slf4j
@RestController
@RequestMapping("/upload")
@Tag(name = "文件上传", description = "文件上传相关接口")
public class FileUploadController {
    
    @Autowired
    private FileUploadService fileUploadService;
    
    /**
     * 上传图片
     * 
     * @param file 图片文件
     * @return 上传结果
     */
    @PostMapping("/image")
    @Operation(summary = "上传图片", description = "上传图片文件，支持jpg、jpeg、png、gif、webp格式，最大10MB")
    public Result<Map<String, Object>> uploadImage(@RequestParam("file") MultipartFile file) {
        log.info("收到图片上传请求 - 文件名: {}, 文件大小: {} bytes", 
                file.getOriginalFilename(), file.getSize());
        
        Map<String, Object> result = fileUploadService.uploadImage(file);
        
        if ((Boolean) result.get("success")) {
            return ResultUtil.success(result);
        } else {
            return ResultUtil.fail((String) result.get("message"));
        }
    }
    
    /**
     * 删除文件
     * 
     * @param fileUrl 文件URL
     * @return 删除结果
     */
    @DeleteMapping("/file")
    @Operation(summary = "删除文件", description = "根据文件URL删除文件")
    public Result<String> deleteFile(@RequestParam("fileUrl") String fileUrl) {
        log.info("收到文件删除请求 - 文件URL: {}", fileUrl);
        
        boolean success = fileUploadService.deleteFile(fileUrl);
        
        if (success) {
            return ResultUtil.success("文件删除成功");
        } else {
            return ResultUtil.fail("文件删除失败");
        }
    }
}
